#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stu {
	char name[20];
	int age;
};

int sort_int(const void* e1, const void* e2) {
	return *(int*)e1 - *(int*)e2;
}


int sort_char(const void* e1, const void* e2) {
	return strcmp( ((struct stu*)e1)->name, ((struct stu*)e2)->name );
}

void swap(char* num1, char* num2,size_t width) {
	size_t i = 0;
	for (i = 0; i < width; i++) {
		int tmp = *num2;
		*num2 = *num1;
		*num1 = tmp;
		num1++;
		num2++;
	}
}

void bubble_sort(const void* sor, size_t sz, size_t width, int (*sort)(const void*, const void*)) {
	size_t i = 0, 
		   j = 0;
	for (i = 0; i < sz - 1; i++) {
		for (j = 0; j < sz - 1 - i; j++) {
			if ( sort( ( (char*)sor + j * width), ( (char*)sor + (j + 1) * width) )>0 ) {
				swap(((char*)sor + j * width), ((char*)sor + (j + 1) * width), width);
			}
		}
	}
}

void test1() {
	int num[] = { 2,1,3,5,7,3,6 };
	size_t sz = sizeof(num) / sizeof(num[0]);
	bubble_sort(num, sz, sizeof(num[0]), sort_int);
	for (int i = 0; i < sz; i++) {
		printf("%d ",num[i]);
	}
}

void test2() {
	struct stu peo[] = {{"zhangssan",93}, {"lisi",39}, {"wangwu",56}};
	size_t sz = sizeof(peo) / sizeof(peo[0]);
	bubble_sort(peo, sz, sizeof(peo[0]), sort_char);
	for (int i = 0; i < sz; i++) {
		printf("%s ", peo[i].name);
	}
}

int main() {
	test1();
	test2();
}